package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("服务中心"+dept);
        log.info("部门添加：{}",dept);
        deptService.add(dept);

        return Result.success();
    }
@GetMapping
    public Result page(String name , Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
    //System.out.println("列表查询: " + name+" " + status+ " " + page + " " + pageSize);
    log.info("列表查询: {} {} {} {}",name,status,page,pageSize);
    PageResult<Dept> pageResult = deptService.list(name, status, page, pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
    //System.out.println("查询: " + id);
    log.info("查询: {}",id);
    Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result update(@RequestBody Dept dept){
       // System.out.println("修改: " + dept);
        log.info("修改: {}",dept);
        deptService.update(dept);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        //System.out.println("删除: " + id);
        log.info("删除: {}",id);
        deptService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(String name, Integer status, LocalDateTime createTime, LocalDateTime updateTime){
        log.info("查询所有部门: name = {}, status = {}, createTime = {}, updateTime = {}",
                name,status,createTime,updateTime);
        List<Dept> list1 = deptService.list(name,status,createTime,updateTime);
        return Result.success(list1);
    }
}
